package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.AnalysisByDay;
import com.itheima.mapper.AnalysisByDayMapper;
import com.itheima.mapper.LogMapper;
import com.itheima.util.ComputeUtil;
import com.itheima.vo.AnalysisSummaryVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@DubboService
public class AnalysisByDayServiceimpl implements AnalysisByDayService {
    @Autowired
    private LogMapper logMapper;
    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;
    //添加后台首页数据
    @Override
    public void logToAnalysisByDay() {
        //今天的日期
        String today = DateUtil.offsetDay(new Date(),0).toDateStr();
        //昨天
        String yesterday = DateUtil.offsetDay(new Date(),-1).toDateStr();
        //今日新增用户数量
        Integer newUsersToday = logMapper.findByTypeAndTime("0102", today);
        // 活跃
        Integer numActive = logMapper.findByTime(today);
        // 登录
        Integer numLogin = logMapper.findByTypeAndTime("0101", today);
        // 2.4次日留存
        Integer numRetention1d = logMapper.findNumRetention1d(today, yesterday);
        //查询今天是否有数据
        QueryWrapper<AnalysisByDay> qw = new QueryWrapper<>();
        qw.eq("record_date",today);
        AnalysisByDay analysisByDay = analysisByDayMapper.selectOne(qw);
        //判断
        if (analysisByDay==null) {
            analysisByDay = new AnalysisByDay();
            analysisByDay.setRecordDate(new Date());
            analysisByDay.setNumRegistered(newUsersToday);
            analysisByDay.setNumActive(numActive);
            analysisByDay.setNumLogin(numLogin);
            analysisByDay.setNumRetention1d(numRetention1d);
            analysisByDayMapper.insert(analysisByDay);
        }else {
        analysisByDay.setRecordDate(new Date());
        analysisByDay.setNumRegistered(newUsersToday);
        analysisByDay.setNumActive(numActive);
        analysisByDay.setNumLogin(numLogin);
        analysisByDay.setNumRetention1d(numRetention1d);
        analysisByDayMapper.updateById(analysisByDay);
        }


    }
    //统计数据
    @Override
    public AnalysisSummaryVo findAnalysisSummaryVo() {

        // 1.准备基础数据
        // 1.1 今天
        String today = DateUtil.offsetDay(new Date(), 0).toDateStr();
        // 1.2 昨天
        String yesterday = DateUtil.offsetDay(new Date(), -1).toDateStr();
        // 1.3 前七天
        String weekday = DateUtil.offsetDay(new Date(), -7).toDateStr();
        // 1.4 前30天
        String monthday = DateUtil.offsetDay(new Date(), -30).toDateStr();
        // 1.5 今日内容
        AnalysisByDay todayAna = analysisByDayMapper.findBase(today);
        // 1.6 昨天内容
        AnalysisByDay yesterdayAna = analysisByDayMapper.findBase(yesterday);

        // 2.凑齐9份数据
        // 2.1 累计用户数
        Long cumulativeUsers = analysisByDayMapper.findCumulativeUsers();
        // 2.2 过去30天活跃用户数
        Long activePassMonth = analysisByDayMapper.findActivePass(monthday, today);
        // 2.3 过去7天活跃用户
        Long activePassWeek = analysisByDayMapper.findActivePass(weekday, today);
        // 2.4 今日新增用户数量
        Integer newUsersToday = todayAna.getNumRegistered();
        // 2.5 今日新增用户涨跌率，单位百分数，正数为涨，负数为跌   BigDecimal : 商业数字格式
        Integer yesterdayReg = yesterdayAna.getNumRegistered();
        BigDecimal newUsersTodayRate = ComputeUtil.computeRate(newUsersToday, yesterdayReg);
        // 2.6 今日登录次数
        Integer loginTimesToday = todayAna.getNumLogin();
        // 2.7 今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        Integer yesterdayLogin = yesterdayAna.getNumLogin();
        BigDecimal loginTimesTodayRate = ComputeUtil.computeRate(loginTimesToday, yesterdayLogin);
        // 2.8 今日活跃用户数量
        Integer activeUsersToday = todayAna.getNumActive();
        // 2.9 今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        Integer yesterdayActive = yesterdayAna.getNumActive();
        BigDecimal activeUsersTodayRate = ComputeUtil.computeRate(activeUsersToday, yesterdayActive);

        // 3.封装并返回vo
        AnalysisSummaryVo vo = new AnalysisSummaryVo();
        vo.setCumulativeUsers(cumulativeUsers);
        vo.setActivePassMonth(activePassMonth);
        vo.setActivePassWeek(activePassWeek);
        vo.setNewUsersToday(newUsersToday.longValue());
        vo.setNewUsersTodayRate(newUsersTodayRate);
        vo.setLoginTimesToday(loginTimesToday.longValue());
        vo.setLoginTimesTodayRate(loginTimesTodayRate);
        vo.setActiveUsersToday(activeUsersToday.longValue());
        vo.setActiveUsersTodayRate(activeUsersTodayRate);

        return vo;
    }
}
